package cn.tedu.csmall.product.service.impl;

import cn.tedu.csmall.commons.ex.ServiceException;
import cn.tedu.csmall.commons.web.ServiceCode;
import cn.tedu.csmall.product.mapper.AlbumMapper;
import cn.tedu.csmall.product.mapper.PictureMapper;
import cn.tedu.csmall.product.mapper.SKUMapper;
import cn.tedu.csmall.product.mapper.SPUMapper;
import cn.tedu.csmall.product.pojo.entity.Album;
import cn.tedu.csmall.product.pojo.entity.Picture;
import cn.tedu.csmall.product.pojo.entity.SKU;
import cn.tedu.csmall.product.pojo.entity.SPU;
import cn.tedu.csmall.product.pojo.param.AlbumAddNewParam;
import cn.tedu.csmall.product.pojo.param.AlbumUpdateInfoParam;
import cn.tedu.csmall.product.pojo.vo.AlbumListItemVO;
import cn.tedu.csmall.product.pojo.vo.AlbumStandardVO;
import cn.tedu.csmall.commons.pojo.vo.PageData;
import cn.tedu.csmall.product.service.IAlbumService;
import cn.tedu.csmall.commons.util.PageInfoToPageDataConverter;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Service
public class AlbumServiceImpl implements IAlbumService {

    @Autowired
    private AlbumMapper albumMapper;

    @Autowired
    private PictureMapper pictureMapper;

    @Autowired
    private SKUMapper skuMapper;

    @Autowired
    private SPUMapper spuMapper;

    //添加相册
    public void addNew(AlbumAddNewParam albumAddNewParam){
        log.debug("开始处理【添加相册】的业务，参数：{}" ,albumAddNewParam);
        QueryWrapper<Album> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name",albumAddNewParam.getName());
        int countByName = albumMapper.selectCount(queryWrapper);
        log.debug("根据相册名称统计匹配的相册数量，结果：{}" , countByName);
        if (countByName>0){
            String message = "添加相册失败,相册名称已被占用";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT,message);

        }

        Album album = new Album();
        BeanUtils.copyProperties(albumAddNewParam,album);
        album.setGmtCreate(LocalDateTime.now());
        album.setGmtModified(LocalDateTime.now());
        log.debug("准备将新的相册数据写入到数据库，数据：{}" , album);
        int insert = albumMapper.insert(album);

        if (insert != 1) {
            String message = "添加失败，服务器忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_INSERT,message);
        }

        log.debug("将新的相册数据写入到数据库，完成！");
    }

    //删除相册
    @Override
    public void delete(Long albumId) {
        log.debug("开始处理【通过id删除相册】的业务，参数：{}" ,albumId);
        //if (id<=0)在参数中配置此情况
        //判断要删除的相册存不存在。
        QueryWrapper<Album> qwa = new QueryWrapper<>();
        qwa.eq("id", albumId);
        int selectCount = albumMapper.selectCount(qwa);
        log.debug("根据相册id查询的数量:{}",selectCount);
        if (selectCount == 0) {
            String message = "所要删除的相册不存在，删除失败！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }

        //判断pms_picture和所要删除的相册有没有关联
        QueryWrapper<Picture> qwp = new QueryWrapper<>();
        qwp.eq("album_id", albumId);
        int selectPictureCount = pictureMapper.selectCount(qwp);
        log.debug("根据相册id查询关联的照片数量:{}",selectPictureCount);
        if (selectPictureCount != 0) {
            String message = "所要删除的相册存在关联，删除失败！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
        }

        //判断pms_sku和所要删除的相册有没有关联
        QueryWrapper<SKU> qwSKU = new QueryWrapper<>();
        qwSKU.eq("album_id", albumId);
        int selectSKUCount = skuMapper.selectCount(qwSKU);
        log.debug("根据相册id查询得到的sku数据量为：{}",selectSKUCount);
        if (selectSKUCount != 0) {
            String message = "pms_sku表和相册存在关联，删除失败！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
        }

        //判断pms_spu和所要删除的相册有没有关联
        QueryWrapper<SPU> qwSPU = new QueryWrapper<>();
        qwSPU.eq("album_id", albumId);
        int selectSPUCount = spuMapper.selectCount(qwSPU);
        log.debug("根据相册id查询得到的spu数据量为：{}",selectSPUCount);
        if (selectSPUCount != 0) {
            String message = "pms_spu表和相册存在关联，删除失败！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
        }

        int i = albumMapper.deleteById(albumId);

        if (i != 1) {
            String message = "删除失败，服务器忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_DELETE,message);
        }

        log.debug("删除成功！");
    }

    //更新相册
    @Override
    public void updateInfoById(Long id, AlbumUpdateInfoParam albumUpdateInfoParam) {
        log.debug("开始处理【通过id修改相册】的业务，参数：{}" ,id);
        //if (id<=0)在参数中配置此情况
        //查询相册中是都有此相册id
        QueryWrapper<Album> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", id);
        int countById = albumMapper.selectCount(queryWrapper);
        log.debug("根据相册id统计匹配的相册数量，结果：{}" , countById);
        if (countById==0){
            String message = "更新失败，没有此相册";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }

        //查询修改的相册名称在相册表中是否已经被占用
        QueryWrapper<Album> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.eq("name", albumUpdateInfoParam.getName()).ne("id", id);
        int countByName = albumMapper.selectCount(queryWrapper2);
        log.debug("根据相册id不同，但是名字相同统计匹配的相册数量，结果：{}" , countByName);
        if (countByName != 0) {
            String message = "更新失败，此相册名已经被占用！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT, message);
        }

        //开始更新相册信息
        log.debug("检测通过，开始修改相册信息：");
        Album album = new Album();
        BeanUtils.copyProperties(albumUpdateInfoParam, album);
        album.setId(id);
        album.setGmtModified(LocalDateTime.now());
        int i = albumMapper.updateById(album);

        if (i != 1) {
            String message = "修改失败，服务器忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_UPDATE,message);
        }

        log.debug("通过id修改相册数据，完成！");
    }

    //根据ID查询相册
    @Override
    public AlbumStandardVO getStandardById(Long id) {
        log.debug("开始根据id查询相册：");
        AlbumStandardVO queryResult = albumMapper.getStandardById(id);
        if (queryResult == null) {
            String message = "不存在该id的相册！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }
        return queryResult;
    }

    //查询相册列表
    @Override
    public PageData<AlbumListItemVO> selectListItem(Integer pageNum) {
        Integer pageSize = 5;
        return selectListItem(pageNum, pageSize);
    }

    //查询相册列表
    @Override
    public PageData<AlbumListItemVO> selectListItem(Integer pageNum, Integer pageSize) {
        log.debug("开始处理【查询相册列表】的业务，页码：{}，每页记录数：{}", pageNum, pageSize);
        PageHelper.startPage(pageNum, pageSize);
        List<AlbumListItemVO> list = albumMapper.selectListItem();
        PageInfo<AlbumListItemVO> pageInfo = new PageInfo<>(list);
        PageData<AlbumListItemVO> pageData = PageInfoToPageDataConverter.convert(pageInfo);
        log.debug("查询完成，即将返回：{}", pageData);
        return pageData;
    }

}
